<template>
  <el-card class="mt-20" shadow="hover">
    <el-form inline>
      <el-form-item>
        <el-input v-model="roleName" placeholder="角色名称" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="Search"
          @click="getRoleList"
          v-permission="'look'"
          >查询</el-button
        >
        <el-button @click="reset">清空</el-button>
      </el-form-item>
    </el-form>
    <div class="mt-10">
      <el-button
        type="primary"
        @click="showAddRole"
        v-permission="'btn.Role.add'"
        >添加</el-button
      >
      <el-button type="danger" :disabled="true" v-permission="'btn.Role.remove'"
        >批量删除</el-button
      >
    </div>
    <el-table
      :data="roleList"
      style="width: 100%"
      border
      class="mt-20"
      :loading="loading"
    >
      <el-table-column type="selection" width="60" />
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column label="操作" width="300" fixed="right" align="center">
        <template v-slot="{ row }">
          <el-button
            type="info"
            size="small"
            :icon="User"
            title="分配权限"
            @click="showRoleAuth(row)"
            v-permission="'btn.Role.assgin'"
          />
          <el-button type="primary" size="small" :icon="Select" title="确定" />
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            title="修改角色"
            @click="showUpdateRole(row)"
            v-permission="'btn.Role.update'"
          />
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            :icon="Warning"
            icon-color="#626AEF"
            :title="`你确定要删除 ${row.roleName}吗?`"
            @confirm="showDeleteRole(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :icon="Delete"
                title="删除角色"
                v-permission="'btn.Role.remove'"
              />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt-20"
      :total="total"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      @current-change="getRoleList"
      @size-change="getRoleList"
      layout="prev,pager,next,jumper,->,sizes,total"
    ></el-pagination>

    <el-dialog
      v-model="addOrUpdateRoleVisible"
      :title="`${addOrUpdateRoleFormData.id !== '' ? '修改' : '添加'}角色`"
    >
      <el-form
        :model="addOrUpdateRoleFormData"
        ref="addOrUpdateRoleFormRef"
        :rules="addOrUpdateRoleFormRules"
        label-width="150"
      >
        <el-form-item label="请输入新名称" prop="roleName">
          <el-input
            v-model="addOrUpdateRoleFormData.roleName"
            placeholder="角色名称"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addOrUpdateRoleVisible = false">关闭</el-button>
          <el-button type="primary" @click="addOrUpdateRole"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XRole",
};
</script>

<script lang="ts" setup>
import {
  Search,
  User,
  Edit,
  Delete,
  Warning,
  Select,
} from "@element-plus/icons-vue";
import { ref, onMounted, reactive } from "vue";
import {
  getRoleListApi,
  addRoleApi,
  updateRoleApi,
  removeRoleApi,
} from "@/api/acl/role";
import type { RoleList, RoleItem } from "@/api/acl/model/roleModel";
import { ElMessage, rowContextKey } from "element-plus";
import { useRouter } from "vue-router";
// 清空
const reset = () => {
  roleName.value = "";
  getRoleList();
};
/********************************  分配权限  ********************************/
const router = useRouter();
const showRoleAuth = (row: RoleItem) => {
  router.push({
    name: "RoleAuth",
    query: {
      id: row.id,
      roleName: row.roleName,
    },
  });
};
/********************************  删除  ********************************/
const showDeleteRole = async (id: string) => {
  await removeRoleApi(id);
  ElMessage.success("删除角色成功~");
  getRoleList();
};
/*****************************  添加&修改  ********************************/
const addOrUpdateRoleFormRef = ref();
const addOrUpdateRoleFormData = reactive<{
  id: string;
  roleName: string;
}>({ id: "", roleName: "" });
const addOrUpdateRoleFormRules = reactive({
  roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});
const addOrUpdateRoleVisible = ref(false);
// 添加
const showAddRole = () => {
  addOrUpdateRoleVisible.value = true;
  addOrUpdateRoleFormData.id = "";
  addOrUpdateRoleFormData.roleName = "";
  addOrUpdateRoleFormRef.value?.clearValidate();
};
// 修改
const showUpdateRole = (row: RoleItem) => {
  addOrUpdateRoleVisible.value = true;
  addOrUpdateRoleFormData.id = row.id;
  addOrUpdateRoleFormData.roleName = row.roleName;
  addOrUpdateRoleFormRef.value?.clearValidate();
};
// 添加&修改角色
const addOrUpdateRole = async () => {
  addOrUpdateRoleFormRef.value.validate();
  const { id, roleName } = addOrUpdateRoleFormData;
  if (roleName.trim()) {
    if (id) {
      // 修改
      await updateRoleApi(addOrUpdateRoleFormData);
    } else {
      // 添加
      await addRoleApi(roleName);
    }
    addOrUpdateRoleVisible.value = false;
    ElMessage.success(`${id ? "修改" : "添加"}角色成功~`);
    getRoleList();
  }
};
/*****************************  分页获取数据  ********************************/
const roleName = ref("");
const roleList = ref<RoleList>([]);
const loading = ref(false);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(5);
const getRoleList = async () => {
  const res = await getRoleListApi({
    page: currentPage.value,
    limit: pageSize.value,
    roleName: roleName.value,
  });
  roleList.value = res.items;
  total.value = res.total;
};
onMounted(getRoleList);
</script>

<style scoped></style>
